package com.byd.sealmanage.web;

import com.alibaba.fastjson.JSONObject;
import com.byd.sealmanage.modal.po.TbUser;
import com.byd.sealmanage.service.LoginService;
import com.byd.sealmanage.service.UserService;
import com.byd.sealmanage.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    /**
     * 登陆输入信息验证
     * @return
     */
    @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
    public JSONObject loginVerify (@RequestBody Map<String,String> map) {
        return loginService.checkLoginInfo(map.get("username"), map.get("password"));
    }

    /**
     * 获取左侧菜单
     * @return
     */
    @RequestMapping(value = "/leftMenu",method = RequestMethod.GET)
    public Map<String,Object> leftMenu(){
        TbUser userEntity = ShiroUtils.getUserEntity();
        return loginService.getMenus(userEntity.getRoles());
    }

    /**
     * 模板文件下载
     * @return
     */
    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public ResponseEntity<byte[]> download(@RequestParam(name = "fileId") String fileId, HttpServletRequest request) throws IOException {
        return loginService.downloadFile(fileId,request);
    }

    /**
     * 获取地区下拉列表
     * @return
     */
    @RequestMapping(value = "/getPlaceOptions",method = RequestMethod.GET)
    public JSONObject getPlaceOptions(){
        return loginService.getPlaceOptionService();
    }
}
